<?php


namespace app\service;

use app\Code;
use app\dao\RolesDao;
use app\dao\RouteRolesDao;
use app\dao\RoutesDao;
use app\dao\UserDao;
use app\lib\exception\ServiceException;
use app\utils\AliyunIDCard;
use app\utils\AliyunSms;
use app\utils\JWTUtil;
use app\utils\wx\WXMiniLogin;
use think\facade\Cache;


class AuthService
{
    public function __construct()
    {
        $this->userDao = new UserDao();
        $this->rolesDao = new RolesDao();
        $this->routesDao = new RoutesDao();
        $this->routesRolesDao = new RouteRolesDao();
    }

    public function setToken($user)
    {
        $token = (new JWTUtil())->signToken($user->id);

        Cache::store('redis')->set($token, $user);

        return $token;
    }

    public function getToken($token)
    {
        return Cache::store('redis')->get($token);
    }

    public function wxLogin($param)
    {
        $WXMiniLogin = new WXMiniLogin();

        $session = $WXMiniLogin->jscode2session($param['js_code']);

        $user = $this->userDao
            ->customQuery()
            ->filterByOpenId($session)
            ->getOne();

        if ($user == null){
            $user = $this->userDao->save([
                'username' => $param['username'],
                'wx_code' => $session,
                'head_img' => $param['head_img'],
                'role' => $this->rolesDao->customQuery()->filterByName('用户')->getOne()->id ?? 3,
            ]);
        }

        return ['token' => $this->setToken($user), 'user' => $user];
    }

    public function checkingRole($token, $method, $root, $url)
    {
        $user = $this->getToken($token);
        if ($user == null) return null;

        $role = $this->routesRolesDao->customQuery()
            ->filterByRoleId($user->role)
            ->filterByRouteId(
                $this->routesDao->customQuery()
                    ->filterByMethod($method)
                    ->filterByUrl($root.'/'.$url)
                    ->getOne()
                    ->id ?? 0
            )
            ->getOne();

        if ($role == null) return null;

        return $user;
    }

    public function adminLogin($username, $password)
    {
        $user = $this->userDao
            ->customQuery()
            ->filterByUsername($username)
            ->getOne();

        if ($user == null || !$user->validatePassword($password)){
            throw new ServiceException("账号或密码错误", Code::PARAM_EXCEPTION);
        }

        return ['token' => $this->setToken($user), 'user' => $user];
    }

    public function setSms($code, $user_id){
        Cache::store('redis')->set(md5($code).md5($user_id), $code, 3*6000);
    }

    public function validateSms($code, $user_id){
        return $code == Cache::store('redis')->get(md5($code).md5($user_id));
    }

    public function sendSms($phone, $user_id)
    {
        $code = (new AliyunSms())->sendSms($phone);

        $this->setSms($code, $user_id);
    }

    public function validateIDCard($IDCard)
    {
        $response = (new AliyunIDCard())->getIDCard($IDCard);

        return $response;
    }


}
